﻿using System; 
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Matrix.Common.DBUtility;
using Matrix.MaKaYunShang.Model;
using Matrix.MaKaYunShang.Model.Dto;

namespace Matrix.MaKaYunShang.DAL
{
	public partial class OrderCommentDAL : BaseDAL<OrderCommentDAL>
	{
		#region 默认方法
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(OrderComment model)
		{
			var strSql=new StringBuilder();
			strSql.Append("insert into OrderComment(");			
            strSql.Append("OrderId,ConsumerId,ShopId,ReplyContent,Points,DealTime,DealResult,IsShopMistake,AddTime,IsDelete");
			strSql.Append(") values (");
            strSql.Append("@OrderId,@ConsumerId,@ShopId,@ReplyContent,@Points,@DealTime,@DealResult,@IsShopMistake,@AddTime,@IsDelete");            
            strSql.Append(") ");            
            strSql.Append(";select @@IDENTITY");		
			SqlParameter[] parameters = {
			            new SqlParameter("@OrderId", SqlDbType.Int,4) ,            
                        new SqlParameter("@ConsumerId", SqlDbType.Int,4) ,            
                        new SqlParameter("@ShopId", SqlDbType.Int,4) ,            
                        new SqlParameter("@ReplyContent", SqlDbType.NVarChar,512) ,            
                        new SqlParameter("@Points", SqlDbType.Int,4) ,            
                        new SqlParameter("@DealTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@DealResult", SqlDbType.NVarChar,512) ,            
                        new SqlParameter("@IsShopMistake", SqlDbType.Bit,1) ,            
                        new SqlParameter("@AddTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@IsDelete", SqlDbType.Bit,1)             
              
            };
			            
            parameters[0].Value = model.OrderId;                        
            parameters[1].Value = model.ConsumerId;                        
            parameters[2].Value = model.ShopId;                        
            parameters[3].Value = model.ReplyContent;                        
            parameters[4].Value = model.Points;                        
            parameters[5].Value = model.DealTime;                        
            parameters[6].Value = model.DealResult;                        
            parameters[7].Value = model.IsShopMistake;                        
            parameters[8].Value = model.AddTime;                        
            parameters[9].Value = model.IsDelete;            			var obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);			
			return obj == null ? 0 : Convert.ToInt32(obj);
		}
				
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(OrderComment model)
		{
			var strSql=new StringBuilder();
			strSql.Append("update OrderComment set ");
			                                                
            strSql.Append(" OrderId = @OrderId , ");                                    
            strSql.Append(" ConsumerId = @ConsumerId , ");                                    
            strSql.Append(" ShopId = @ShopId , ");                                    
            strSql.Append(" ReplyContent = @ReplyContent , ");                                    
            strSql.Append(" Points = @Points , ");                                    
            strSql.Append(" DealTime = @DealTime , ");                                    
            strSql.Append(" DealResult = @DealResult , ");                                    
            strSql.Append(" IsShopMistake = @IsShopMistake , ");                                    
            strSql.Append(" AddTime = @AddTime , ");                                    
            strSql.Append(" IsDelete = @IsDelete  ");            			
			strSql.Append(" where Id=@Id ");
						
			SqlParameter[] parameters = {
			            new SqlParameter("@Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@OrderId", SqlDbType.Int,4) ,            
                        new SqlParameter("@ConsumerId", SqlDbType.Int,4) ,            
                        new SqlParameter("@ShopId", SqlDbType.Int,4) ,            
                        new SqlParameter("@ReplyContent", SqlDbType.NVarChar,512) ,            
                        new SqlParameter("@Points", SqlDbType.Int,4) ,            
                        new SqlParameter("@DealTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@DealResult", SqlDbType.NVarChar,512) ,            
                        new SqlParameter("@IsShopMistake", SqlDbType.Bit,1) ,            
                        new SqlParameter("@AddTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@IsDelete", SqlDbType.Bit,1)             
              
            };
						            
            parameters[0].Value = model.Id;                        
            parameters[1].Value = model.OrderId;                        
            parameters[2].Value = model.ConsumerId;                        
            parameters[3].Value = model.ShopId;                        
            parameters[4].Value = model.ReplyContent;                        
            parameters[5].Value = model.Points;                        
            parameters[6].Value = model.DealTime;                        
            parameters[7].Value = model.DealResult;                        
            parameters[8].Value = model.IsShopMistake;                        
            parameters[9].Value = model.AddTime;                        
            parameters[10].Value = model.IsDelete;                        return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
		}
				
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int Id)
		{
			var strSql=new StringBuilder();
			strSql.Append("update OrderComment set IsDelete=1 ");
			strSql.Append(" where Id=@Id");
						SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
		}
		
				/// <summary>
		/// 批量删除一批数据
		/// </summary>
		public bool DeleteList(string Idlist )
		{
			var strSql=new StringBuilder();
			strSql.Append("update OrderComment set IsDelete=1 ");
			strSql.Append(" where Id in ("+Idlist + ")  ");
			return DbHelperSQL.ExecuteSql(strSql.ToString()) > 0;
		}
						
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public DataSet GetModel(int Id)
		{
			var strSql=new StringBuilder();
			strSql.Append("select Id, OrderId, ConsumerId, ShopId, ReplyContent, Points, DealTime, DealResult, IsShopMistake, AddTime, IsDelete  ");			
			strSql.Append("  from OrderComment ");
			strSql.Append(" where Id=@Id  and IsDelete=0");
						SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;
			
			var ds = DbHelperSQL.Query(strSql.ToString(),parameters);			
			return ds.Tables[0].Rows.Count>0 ? ds : null;
		}
				
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			var strSql=new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM OrderComment where IsDelete=0 ");
			if(!string.IsNullOrEmpty(strWhere))
			{
				strSql.Append(" and "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}		
		#endregion
		
		#region 扩展方法
        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <returns></returns>
        public List<ViewOrderCommentDto> GetPagingList(int pageIndex, int pageSize, string searchKeyWord, string colOrder, ref int totalCount)
        {
            string strWhere = " IsDelete = 0 ";

            if (!string.IsNullOrEmpty(searchKeyWord))
            {
                strWhere += " AND (OrderNo LIKE '%" + searchKeyWord + "%' OR NickName LIKE '%" + searchKeyWord +
                            "%' OR shopName LIKE '%" + searchKeyWord + "%' OR ReplyContent LIKE '%" + searchKeyWord +
                            "%' OR Points LIKE '%" + searchKeyWord + "%' OR AddTime LIKE '%" + searchKeyWord + "%') ";
            }
            const string table =
  "(SELECT oc.*,o.OrderNo,s.Name as shopName,c.NickName, (CASE   WHEN  Points>0 and Points<3 THEN '差评' WHEN Points=3 THEN '中评' WHEN Points>3 and Points<6 THEN '好评' END) as PointsStr" +
  " FROM [OrderComment]  oc left join orders o on o.id=oc.OrderId " +
  " left join Consumer c on c.id=oc.ConsumerId   left join Shops s on s.id=c.shopId where oc.IsDelete=0   ) n ";
            return GetSimpleListPage<ViewOrderCommentDto>(pageIndex, pageSize, table, null, null, colOrder, strWhere,
                ref totalCount);
        }

        /// <summary>
        /// 根据订单Id得到一个对象实体
        /// </summary>
        /// <remarks>2016-09-02 万乐园 创建</remarks>
        public DataSet GetModelByOrderId(int orderId)
        {
            var strSql = new StringBuilder();
            strSql.Append("select * from OrderComment ");
            strSql.Append(" where OrderId=@OrderId and IsDelete=0");
            SqlParameter[] parameters = {
				new SqlParameter("@OrderId", SqlDbType.Int,4)
			};
            parameters[0].Value = orderId;
            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            return ds.Tables[0].Rows.Count > 0 ? ds : null;
        }
		#endregion
	}
}
